package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.CategoryConverter;
import com.youlai.system.mapper.SysCategoryMapper;
import com.youlai.system.model.entity.SysCategory;
import com.youlai.system.model.form.CategoryForm;
import com.youlai.system.model.query.CategoryPageQuery;
import com.youlai.system.model.vo.CategoryPageVO;
import com.youlai.system.service.SysCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程分类业务处理实现
 */
@Service
@RequiredArgsConstructor
public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements SysCategoryService {

    private final CategoryConverter categoryConverter;



    @Override
    public SysCategory getByContentEqual(String content) {
        LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCategory::getContent, content);
        return getOne(queryWrapper);
    }

    @Override
    public SysCategory saveByContent(String content) {
        SysCategory category = new SysCategory();
        category.setContent(content);
        save(category);
        return category;
    }

    @Override
    public Page<CategoryPageVO> getCategoryPage(CategoryPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(keywords), SysCategory::getContent, keywords);
        //查询数据
        Page<SysCategory> categoryPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        return categoryConverter.entity2Page(categoryPage);
    }

    @Override
    public boolean saveCategory(CategoryForm categoryForm) {
        String content = categoryForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getContent, content));
        Assert.isTrue(count == 0, "分类已存在");

        // 实体转换
        SysCategory category = categoryConverter.form2Entity(categoryForm);
        return save(category);
    }

    @Override
    public boolean updateCategory(Long categoryId, CategoryForm categoryForm) {
        String content = categoryForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysCategory>()
                .eq(SysCategory::getContent, content)
                .ne(SysCategory::getId, categoryId)
        );
        Assert.isTrue(count == 0, "分类已存在");

        // form -> entity
        SysCategory entity = categoryConverter.form2Entity(categoryForm);

        // 修改分类
        return this.updateById(entity);
    }

    @Override
    public CategoryForm getCategoryForm(Long categoryId) {
        SysCategory entity = this.getById(categoryId);
        return categoryConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteCategories(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "分类删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteCategories(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<Option> listCategoryOptions() {
        // 查询数据
        List<SysCategory> categoryList = this.list(new LambdaQueryWrapper<SysCategory>()
                .select(SysCategory::getId, SysCategory::getContent)
                .orderByAsc(SysCategory::getSort)
        );

        // 实体转换
        return categoryConverter.entities2Options(categoryList);
    }
}